import { defineStore } from 'pinia'

export interface breadcrumbItem {
  name: string
  url: string
}

export const useBreadcrumbStore = defineStore('breadcrumbStore', {
  state: () => {
    return {
      items: [] as Array<breadcrumbItem>,
    }
  },
  actions: {
    pushItem(item: breadcrumbItem) {
      if (!this.items) this.items = []
      this.items.push(item)
    },
    moveToItem(item: breadcrumbItem) {
      const oldItems = this.items
      this.items = []
      for (let i = 0; i < oldItems.length; i++) {
        const oldItem = oldItems[i]
        this.items.push(oldItem)
        if (item == oldItem) return
      }
    },
    clearAll() {
      this.items = []
    },
  },
})
